<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../libs/vue.js"></script>
</head>

<body>
    <div id="app">
        <table border="1" style="border-collapse: collapse;" width="400" height="300">
            <tr>
                <th>
                    全选
                    <input @change="allCheckChange" type="checkbox" v-model="allChecked">
                </th>
                <th>ID</th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品数量</th>
            </tr>
            <tr v-for="(item,index) in goodsList" :key="item.id">
                <td>
                    <input type="checkbox" v-model="item.checked">
                </td>
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>&yen;{{item.price}}</td>
                <td>
                    <button @click="reduce(index)" :disabled="item.num == 0">-</button>
                    {{item.num}}
                    <button @click="add(index)">+</button>
                </td>
            </tr>
        </table>
        <h2>总价：{{totalPrice}}</h2>
    </div>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                allChecked:false,
                goodsList: [
                    {
                        id: 1,
                        name: 'macbook',
                        price: 12999,
                        num: 1,
                        checked: false
                    },
                    {
                        id: 2,
                        name: '小米18',
                        price: 1999,
                        num: 3,
                        checked: false
                    },
                    {
                        id: 3,
                        name: '阿玛尼405',
                        price: 350,
                        num: 2,
                        checked: false
                    }
                ]
            },
            methods: {
                add(i) {
                    this.goodsList[i].num++
                },
                reduce(i) {
                    this.goodsList[i].num--
                },
                allCheckChange(){
                    for(let item of this.goodsList){
                        item.checked = this.allChecked
                    }
                }
            },
            watch:{
                goodsList:{
                    handler(){
                        this.allChecked = this.goodsList.every(item=>item.checked)
                        // this.allChecked = this.goodsList.every(item=>{
                        //     return item.checked == true
                        // })
                    },
                    deep:true
                }
            },
            computed: {
                totalPrice() {
                    let total = 0
                    for (let item of this.goodsList) {
                        // console.log(item.checked)
                        if (item.checked) {
                            total += item.price * item.num
                        }
                    }
                    return total
                }
            }
        })
    </script>
</body>

</html>